package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideoService smallVideoService;

    /**
     * 视频上传
     *
     * @param videoThumbnail 封面图片
     * @param videoFile      视频文件
     * @return
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail,
                                     MultipartFile videoFile) throws IOException {
        smallVideoService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
       /* //防止报错
        page++;*/
        PageResult pr = smallVideoService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 视频关注
     *
     * @return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消视频关注
     *
     * @return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }


    /**
     * 取消视频点赞
     *
     * @param VideoId 视频id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String VideoId) {
        smallVideoService.dislike(VideoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) {
        smallVideoService.like(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 发布评论
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String videoId,
                                   @RequestBody Map map) {
        String ccc = (String) map.get("comment");
        smallVideoService.comments(videoId, ccc);
        return ResponseEntity.ok(null);
    }


    /**
     * 视频评论列表
     * @param videoId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String videoId,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = smallVideoService.page(videoId, page, pagesize);
        return ResponseEntity.ok(pr);
    }
}
